
/**
 * Tests the InventoryItem class
 * @author Andrew Juby
 */
public class Prog9 {

    InventoryItem[] itemArray;

    public Prog9() {
        itemArray = new InventoryItem[10];
        this.itemArray[0] = new InventoryItem(5, "Apple Jacks");
        this.itemArray[1] = new InventoryItem(38, "Pop Tarts");
        this.itemArray[2] = new InventoryItem(15, "Cinnamon Toast Crunch");
        this.itemArray[3] = new InventoryItem(1, "Peanut Butter Crunch");
        this.itemArray[4] = new InventoryItem(200, "French toast");
        this.itemArray[5] = new InventoryItem(42, "Scrambled eggs");
        this.itemArray[6] = new InventoryItem(88, "Bloody Marys");
        this.itemArray[7] = new InventoryItem(111, "Tangerines");
        this.itemArray[8] = new InventoryItem(6, "Golden Grahams");
        this.itemArray[9] = new InventoryItem(99, "Pancakes");
        this.itemArray[10] = new InventoryItem(52, "Eggs over easy");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Prog9 program = new Prog9();
        program.execute();
    }

    private void execute() {
        System.out.println("Pre sort:");
        this.printArray();
        this.insertionSort();
        System.out.println("Post sort:");
        this.printArray();
    }

    private void printArray() {
        for (int i = 0; i < 10; i++) {
            System.out.println(this.itemArray[i]);
        }
    }

    private void insertionSort() {
        InventoryItem temp;
        boolean done = false;
        int i, j;

        for (i = 1; i < this.itemArray.length; i++) {
            temp = this.itemArray[i];
            j = i - 1;
            done = false;
            while (!done) {
                if (this.itemArray[j].compareTo(temp) > 0) {
                    this.itemArray[j + 1] = this.itemArray[j];
                    j--;
                    if (j < 0) {
                        done = true;
                    }
                } else {
                    done = true;
                }
            }
            this.itemArray[j + 1] = temp;
        }
    }
}
